package com.tj.jinzao.presenter;


import android.app.Activity;
import android.util.Log;

import com.tj.jinzao.api.SubscriberCallBack;
import com.tj.jinzao.base.BasePresenter;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Identification;
import com.tj.jinzao.model.Job;
import com.tj.jinzao.model.ResultResponse;
import com.tj.jinzao.utils.ToastUtils;

/**
 * 地址处理
 * Created by wjy 2018/12/10.
 */
public class IdentifyPresenter extends BasePresenter<IUploadView<Identification>> {

    public IdentifyPresenter(IUploadView<Identification> view) {
        super(view);
    }

    public void isRealName() {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.getRealAuth(uId)
                , new SubscriberCallBack<Identification>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Identification identification) {
                        if (mView != null) {
                            mView.onRequestSuccess(identification);
                        }
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }
                });
    }

    public void sendRealName(Activity context, String name, String card, String frontImage, String backImage, String handImage) {
        long uId = UserManager.getInstance().getUser().getId();
        Log.w("身份证认证","wwwww");
        addSubscription(mApiService.sendRealAuth(uId, name, card, frontImage, backImage, handImage)
                , new SubscriberCallBack<Identification>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Identification identification) {
                        if (mView != null) {
                            ToastUtils.showToast(context,"实名认证申请提交成功");
                            context.finish();
                            //下面一行无法回调，原因未知
//                            mView.onRequestSuccess(identification);
                        }
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        Log.w("身份证认证","mmmmmmmmm");
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }

                });
    }

    public void sendJobAuth(String job, String jobItem, String images) {
        long uId = UserManager.getInstance().getUser().getId();
        addSubscription(mApiService.sendJobAuth(uId, job, jobItem, images)
                , new SubscriberCallBack<Job>(mView.getContext()) {
                    @Override
                    protected void onSuccess(Job job) {
                        if (mView != null) {
                            mView.onRequestSuccess(null);
                        }
                    }

                    @Override
                    protected void onFailure(ResultResponse response) {
                        if (mView != null) mView.onRequestError(response.getMessage());
                    }

                    @Override
                    protected boolean isShowProgress() {
                        return true;
                    }

                });
    }

    @Override
    protected void onUploadSuccess(String localPath, String response) {
        if (mView != null) mView.onUploadSuccess(localPath, response);
    }
}